<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【JavaScript】10个清晰实用更显专业的JavaScript代码片段 | Vixcity</title><meta name="keywords" content="代码片段,JS库,JavaScript,ES6"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【JavaScript】10个清晰实用更显专业的JavaScript代码片段这是一些非常酷，非常有用的速记代码片段 使用这些代码片段，可以使你的代码看起来更加的专业 1、单行If-Else语句(三元表达式)我们最常用的可能是这样的if-else语句： 12345if (10 &lt; 100) &amp;#123; console.log(&quot;true&quot;);&amp;#125; else &amp;#1">
<meta property="og:type" content="article">
<meta property="og:title" content="【JavaScript】10个清晰实用更显专业的JavaScript代码片段">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/09/17/%E3%80%90JavaScript%E3%80%9110%E4%B8%AA%E6%B8%85%E6%99%B0%E5%AE%9E%E7%94%A8%E6%9B%B4%E6%98%BE%E4%B8%93%E4%B8%9A%E7%9A%84JavaScript%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="【JavaScript】10个清晰实用更显专业的JavaScript代码片段这是一些非常酷，非常有用的速记代码片段 使用这些代码片段，可以使你的代码看起来更加的专业 1、单行If-Else语句(三元表达式)我们最常用的可能是这样的if-else语句： 12345if (10 &lt; 100) &amp;#123; console.log(&quot;true&quot;);&amp;#125; else &amp;#1">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg">
<meta property="article:published_time" content="2021-09-16T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.321Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="代码片段">
<meta property="article:tag" content="JS库">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="ES6">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/09/17/%E3%80%90JavaScript%E3%80%9110%E4%B8%AA%E6%B8%85%E6%99%B0%E5%AE%9E%E7%94%A8%E6%9B%B4%E6%98%BE%E4%B8%93%E4%B8%9A%E7%9A%84JavaScript%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【JavaScript】10个清晰实用更显专业的JavaScript代码片段',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【JavaScript】10个清晰实用更显专业的JavaScript代码片段</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-16T16:00:00.000Z" title="发表于 2021-09-17 00:00:00">2021-09-17</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.321Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【JavaScript】10个清晰实用更显专业的JavaScript代码片段"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="【JavaScript】10个清晰实用更显专业的JavaScript代码片段"><a href="#【JavaScript】10个清晰实用更显专业的JavaScript代码片段" class="headerlink" title="【JavaScript】10个清晰实用更显专业的JavaScript代码片段"></a>【JavaScript】10个清晰实用更显专业的JavaScript代码片段</h2><p>这是一些非常酷，非常有用的速记代码片段</p>
<p>使用这些代码片段，可以使你的代码看起来更加的专业</p>
<h3 id="1、单行If-Else语句-三元表达式"><a href="#1、单行If-Else语句-三元表达式" class="headerlink" title="1、单行If-Else语句(三元表达式)"></a><strong>1、单行If-Else语句(三元表达式)</strong></h3><p>我们最常用的可能是这样的if-else语句：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="number">10</span> &lt; <span class="number">100</span>) &#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">&quot;true&quot;</span>);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">&quot;false&quot;</span>);</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>输出</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">true</span>  </span><br></pre></td></tr></table></figure>

<p>我们可以通过使用三元表达式，用更简洁的方式来重新编写上面的代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">10</span> &lt;<span class="number">100</span> ？<span class="built_in">console</span>.log（“<span class="literal">true</span>”）：<span class="built_in">console</span>.log（“<span class="literal">false</span>”）  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">true</span>  </span><br></pre></td></tr></table></figure>

<p>通常，三元运算符遵循以下简单模式：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">条件 ? 为真时候的表达式 : 为假时候的表达式</span><br></pre></td></tr></table></figure>

<p>三元运算符也可以链接在一起以形成更长的链。但是，它通常会使代码变得很冗长。明智地使用它们，不会使事情变得更复杂。</p>
<h3 id="2、合并数组-扩展运算符"><a href="#2、合并数组-扩展运算符" class="headerlink" title="2、合并数组(扩展运算符)"></a><strong>2、合并数组(扩展运算符)</strong></h3><p>我们在平常使用中可能需要合并两个数组</p>
<p>那么这个时候我们可以用扩展运算符（…）</p>
<p>将一个数组的元素扩展为另一个数组，例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>];</span><br><span class="line"><span class="keyword">const</span> allNumbers = [...numbers, <span class="number">50</span>, <span class="number">60</span>, <span class="number">70</span>, <span class="number">80</span>];</span><br><span class="line"><span class="built_in">console</span>.log(allNumbers);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">50</span>, <span class="number">60</span>, <span class="number">70</span>, <span class="number">80</span>]  </span><br></pre></td></tr></table></figure>

<p>也可以用concat方法</p>
<p>例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numbers = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>];</span><br><span class="line"><span class="keyword">const</span> numbersa=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="keyword">const</span> newArr = numbers.concat(numbersa)</span><br><span class="line"><span class="built_in">console</span>.log(a)</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br></pre></td></tr></table></figure>

<h3 id="3、从数组中删除重复项-数组去重"><a href="#3、从数组中删除重复项-数组去重" class="headerlink" title="3、从数组中删除重复项(数组去重)"></a><strong>3、从数组中删除重复项(数组去重)</strong></h3><h4 id="ES6-Set-去重"><a href="#ES6-Set-去重" class="headerlink" title="ES6 Set 去重"></a>ES6 Set 去重</h4><p> 不考虑兼容性，这种去重的方法代码最少</p>
<p>这种方法还无法去掉“{}”空对象</p>
<p>后面的高阶方法会添加去掉重复“{}”的方法</p>
<p>这个不会更改与原数组的数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numbers = [<span class="number">1</span>, <span class="number">1</span>, <span class="number">20</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">9</span>];</span><br><span class="line"><span class="keyword">const</span> uniqueNumbers = [...new <span class="built_in">Set</span>(numbers)];</span><br><span class="line"><span class="built_in">console</span>.log(uniqueNumbers);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>, <span class="number">20</span>, <span class="number">3</span>, <span class="number">9</span>]  </span><br></pre></td></tr></table></figure>

<h4 id="利用for嵌套for，然后splice去重"><a href="#利用for嵌套for，然后splice去重" class="headerlink" title="利用for嵌套for，然后splice去重"></a>利用for嵌套for，然后splice去重</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>)</span>&#123;            </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;arr.length; i++)&#123;</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">var</span> j=i+<span class="number">1</span>; j&lt;arr.length; j++)&#123;</span><br><span class="line">   <span class="keyword">if</span>(arr[i]==arr[j])&#123;         <span class="comment">//第一个等同于第二个，splice方法删除第二个</span></span><br><span class="line">    arr.splice(j,<span class="number">1</span>);</span><br><span class="line">    j--;</span><br><span class="line">   &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;true&quot;, 15, false, undefined, NaN, NaN, &quot;NaN&quot;, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]     </span></span><br><span class="line"><span class="comment">//NaN和&#123;&#125;没有去重，两个null直接消失了</span></span><br></pre></td></tr></table></figure>

<p>双层循环，外层循环元素，内层循环时比较值</p>
<p>值相同时，则删去这个值</p>
<p>这个是会更改与原数组的数据的</p>
<h4 id="利用indexOf去重"><a href="#利用indexOf去重" class="headerlink" title="利用indexOf去重"></a>利用indexOf去重</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="comment">// 判断是不是数组，不是的话直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">Array</span>.isArray(arr)) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;type error!&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> array = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (array .indexOf(arr[i]) === -<span class="number">1</span>) &#123;</span><br><span class="line">            array .push(arr[i])</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">// [1, &quot;true&quot;, true, 15, false, undefined, null, NaN, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]  </span></span><br><span class="line"><span class="comment">//NaN、&#123;&#125;没有去重</span></span><br></pre></td></tr></table></figure>

<p>新建一个空的结果数组，for 循环原数组</p>
<p>判断结果数组是否存在当前元素，如果有相同的值则跳过</p>
<p>不相同则push进数组</p>
<p>这个不会更改与原数组的数据</p>
<h4 id="利用sort"><a href="#利用sort" class="headerlink" title="利用sort()"></a>利用sort()</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="comment">// 判断是不是数组，不是的话直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">Array</span>.isArray(arr)) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;type error!&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> <span class="comment">// 排序</span></span><br><span class="line">    arr = arr.sort()</span><br><span class="line">    <span class="keyword">var</span> arrry= [arr[<span class="number">0</span>]];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (arr[i] !== arr[i-<span class="number">1</span>]) &#123;</span><br><span class="line">            arrry.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arrry;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">// [0, 1, 15, &quot;NaN&quot;, NaN, NaN, &#123;…&#125;, &#123;…&#125;, &quot;a&quot;, false, null, true, &quot;true&quot;, undefined]      </span></span><br><span class="line"><span class="comment">//NaN、&#123;&#125;没有去重</span></span><br></pre></td></tr></table></figure>

<p>利用sort()排序方法</p>
<p>然后根据排序后的结果进行遍历及相邻元素比对</p>
<p>不相同则push进新数组</p>
<p>这个不会更改与原数组的数据，但是会改变原数组排序</p>
<h4 id="利用对象的属性不能相同的特点进行去重（这种数组去重的方法有问题，不建议用，有待改进）"><a href="#利用对象的属性不能相同的特点进行去重（这种数组去重的方法有问题，不建议用，有待改进）" class="headerlink" title="利用对象的属性不能相同的特点进行去重（这种数组去重的方法有问题，不建议用，有待改进）"></a>利用对象的属性不能相同的特点进行去重（这种数组去重的方法有问题，不建议用，有待改进）</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="comment">// 判断是不是数组，不是的话直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">Array</span>.isArray(arr)) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;type error!&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> arrry= [];</span><br><span class="line"> <span class="keyword">var</span>  obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!obj[arr[i]]) &#123;</span><br><span class="line">            arrry.push(arr[i])</span><br><span class="line">            obj[arr[i]] = <span class="number">1</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            obj[arr[i]]++</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arrry;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;true&quot;, 15, false, undefined, null, NaN, 0, &quot;a&quot;, &#123;…&#125;]    </span></span><br><span class="line"><span class="comment">//两个true直接去掉了，NaN和&#123;&#125;去重</span></span><br></pre></td></tr></table></figure>

<h4 id="利用includes"><a href="#利用includes" class="headerlink" title="利用includes"></a>利用includes</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="comment">// 判断是不是数组，不是的话直接返回</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">Array</span>.isArray(arr)) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;type error!&#x27;</span>)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> array =[];</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span>( !array.includes( arr[i]) ) &#123;<span class="comment">//includes 检测数组是否有某个值</span></span><br><span class="line">                    array.push(arr[i]);</span><br><span class="line">              &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;true&quot;, true, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]  //&#123;&#125;没有去重</span></span><br></pre></td></tr></table></figure>

<p>检测数组是否有某个值</p>
<p>没有则push进新数组</p>
<p>这个不会更改与原数组的数据</p>
<h4 id="利用hasOwnProperty"><a href="#利用hasOwnProperty" class="headerlink" title="利用hasOwnProperty"></a>利用hasOwnProperty</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">return</span> arr.filter(<span class="function"><span class="keyword">function</span>(<span class="params">item, index, arr</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> obj.hasOwnProperty(<span class="keyword">typeof</span> item + item) ? <span class="literal">false</span> : (obj[<span class="keyword">typeof</span> item + item] = <span class="literal">true</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;true&quot;, true, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;]</span></span><br><span class="line"><span class="comment">//所有的都去重了</span></span><br></pre></td></tr></table></figure>

<p>利用hasOwnProperty 判断是否存在对象属性</p>
<h4 id="利用filter"><a href="#利用filter" class="headerlink" title="利用filter"></a>利用filter</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> arr.filter(<span class="function"><span class="keyword">function</span>(<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素</span></span><br><span class="line">    <span class="keyword">return</span> arr.indexOf(item, <span class="number">0</span>) === index;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;true&quot;, true, 15, false, undefined, null, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]</span></span><br></pre></td></tr></table></figure>

<h4 id="利用递归去重"><a href="#利用递归去重" class="headerlink" title="利用递归去重"></a>利用递归去重</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">var</span> array = arr;</span><br><span class="line"> <span class="keyword">var</span> len = array.length;</span><br><span class="line"></span><br><span class="line">    array.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;   <span class="comment">//排序后更加方便去重</span></span><br><span class="line">        <span class="keyword">return</span> a - b;</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">loop</span>(<span class="params">index</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(index &gt;= <span class="number">1</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(array[index] === array[index-<span class="number">1</span>])&#123;</span><br><span class="line">                array.splice(index,<span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            loop(index - <span class="number">1</span>);    <span class="comment">//递归loop，然后数组去重</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    loop(len-<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">return</span> array;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;a&quot;, &quot;true&quot;, true, 15, false, 1, &#123;…&#125;, null, NaN, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, undefined]</span></span><br></pre></td></tr></table></figure>

<h4 id="利用Map数据结构去重"><a href="#利用Map数据结构去重" class="headerlink" title="利用Map数据结构去重"></a>利用Map数据结构去重</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">arrayNonRepeatfy</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">let</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"> <span class="keyword">let</span> array = <span class="keyword">new</span> <span class="built_in">Array</span>();  <span class="comment">// 数组用于返回结果</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">  <span class="keyword">if</span>(map .has(arr[i])) &#123;  <span class="comment">// 如果有该key值</span></span><br><span class="line">     map .set(arr[i], <span class="literal">true</span>); </span><br><span class="line">  &#125; <span class="keyword">else</span> &#123; </span><br><span class="line">   map .set(arr[i], <span class="literal">false</span>);   <span class="comment">// 如果没有该key值</span></span><br><span class="line">   array .push(arr[i]);</span><br><span class="line">  &#125;</span><br><span class="line"> &#125; </span><br><span class="line"> <span class="keyword">return</span> array ;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line">    <span class="built_in">console</span>.log(unique(arr))</span><br><span class="line"><span class="comment">//[1, &quot;a&quot;, &quot;true&quot;, true, 15, false, 1, &#123;…&#125;, null, NaN, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, undefined]</span></span><br></pre></td></tr></table></figure>

<p>创建一个空Map数据结构，遍历需要去重的数组</p>
<p>把数组的每一个元素作为key存到Map中</p>
<p>由于Map中不会出现相同的key值</p>
<p>所以最终得到的就是去重后的结果</p>
<h3 id="利用reduce-includes"><a href="#利用reduce-includes" class="headerlink" title="利用reduce+includes"></a>利用reduce+includes</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arr.reduce(<span class="function">(<span class="params">prev,cur</span>) =&gt;</span> prev.includes(cur) ? prev : [...prev,cur],[]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">1</span>,<span class="string">&#x27;true&#x27;</span>,<span class="string">&#x27;true&#x27;</span>,<span class="literal">true</span>,<span class="literal">true</span>,<span class="number">15</span>,<span class="number">15</span>,<span class="literal">false</span>,<span class="literal">false</span>, <span class="literal">undefined</span>,<span class="literal">undefined</span>, <span class="literal">null</span>,<span class="literal">null</span>, <span class="literal">NaN</span>, <span class="literal">NaN</span>,<span class="string">&#x27;NaN&#x27;</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>,&#123;&#125;,&#123;&#125;];</span><br><span class="line"><span class="built_in">console</span>.log(unique(arr));</span><br><span class="line"><span class="comment">// [1, &quot;true&quot;, true, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]</span></span><br></pre></td></tr></table></figure>

<h3 id="4、将任何内容转换为布尔值"><a href="#4、将任何内容转换为布尔值" class="headerlink" title="4、将任何内容转换为布尔值"></a><strong>4、将任何内容转换为布尔值</strong></h3><p>除了true和false之外，JavaScript还将其他类型视为真或假。</p>
<ul>
<li>0，””，null，undefined，NaN，和false总是假 。</li>
<li>其他一切都是真实的。</li>
</ul>
<p>正因为如此，在JavaScript中，你可以将任何值转换为true和false与一元运算符（!）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> bool1 = !<span class="number">0</span>;</span><br><span class="line"><span class="comment">// trueconst bool2 = !100;</span></span><br><span class="line"><span class="comment">// falseconst bool3 = !&quot;test&quot;;</span></span><br><span class="line"><span class="comment">// falseconst bool4 = !!&quot;test&quot;;</span></span><br><span class="line"><span class="comment">// trueconsole.log(bool1, bool2, bool3, bool4);  </span></span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">true</span> <span class="literal">false</span> <span class="literal">false</span> <span class="literal">true</span>  </span><br></pre></td></tr></table></figure>

<h3 id="5、交换两个变量而没有第三个变量-解构赋值"><a href="#5、交换两个变量而没有第三个变量-解构赋值" class="headerlink" title="5、交换两个变量而没有第三个变量(解构赋值)"></a><strong>5、交换两个变量而没有第三个变量(解构赋值)</strong></h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> y = <span class="number">2</span>;</span><br><span class="line">[x, y] = [y, x];</span><br><span class="line"><span class="built_in">console</span>.log(x, y);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2</span> <span class="number">1</span>  </span><br></pre></td></tr></table></figure>

<h3 id="6、将数字转换为字符串"><a href="#6、将数字转换为字符串" class="headerlink" title="6、将数字转换为字符串"></a><strong>6、将数字转换为字符串</strong></h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="number">1</span> +“”;</span><br><span class="line"><span class="built_in">console</span>.log（<span class="keyword">typeof</span> num）; </span><br><span class="line"><span class="built_in">console</span>.log（num）;  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">string <span class="number">1</span>  </span><br></pre></td></tr></table></figure>

<h3 id="7、将字符串转换为数字"><a href="#7、将字符串转换为数字" class="headerlink" title="7、将字符串转换为数字"></a><strong>7、将字符串转换为数字</strong></h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> numStr = <span class="string">&quot;124&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> num = +numStr;<span class="built_in">console</span>.log(<span class="keyword">typeof</span> num);<span class="built_in">console</span>.log(num);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">number <span class="number">84</span>  </span><br></pre></td></tr></table></figure>

<h3 id="8、将变量嵌入到字符串-字符串模板"><a href="#8、将变量嵌入到字符串-字符串模板" class="headerlink" title="8、将变量嵌入到字符串(字符串模板)"></a><strong>8、将变量嵌入到字符串(字符串模板)</strong></h3><p>通过使用反引号（`）将字符串括起来并将变量插入之间，将变量整齐地嵌入到字符串之间${}：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> age = <span class="number">41</span>;<span class="keyword">const</span> sentence = <span class="string">`I&#x27;m <span class="subst">$&#123;age&#125;</span> years old`</span>;<span class="built_in">console</span>.log(sentence);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">I<span class="string">&#x27;m 41 years old  </span></span><br></pre></td></tr></table></figure>

<h3 id="9、将字符串拆分为数组-扩展运算符"><a href="#9、将字符串拆分为数组-扩展运算符" class="headerlink" title="9、将字符串拆分为数组(扩展运算符)"></a><strong>9、将字符串拆分为数组(扩展运算符)</strong></h3><p>要将字符串拆分为数组，可以使用扩展运算符（…）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> str = <span class="string">&quot;Test&quot;</span><span class="keyword">const</span> strAsArr = [...str]<span class="built_in">console</span>.log(strAsArr)  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="string">&quot;T&quot;</span>, <span class="string">&quot;e&quot;</span>, <span class="string">&quot;s&quot;</span>, <span class="string">&quot;t&quot;</span>]  </span><br></pre></td></tr></table></figure>

<h3 id="10、可选链接"><a href="#10、可选链接" class="headerlink" title="10、可选链接"></a><strong>10、可选链接</strong></h3><blockquote>
<p><strong><em>“可选的链接运算符（?.）允许读取位于连接对象链深处的属性的值，而不必明确验证链中的每个引用是否有效。” — MDN Web文档</em></strong></p>
</blockquote>
<p>假设你有一个data对象，并且想要安全地访问data.test.value</p>
<p>首先，你需要检查：</p>
<ul>
<li>data 是否被定义。</li>
<li>data.test 是否被定义。</li>
</ul>
<p>在data声明之前，你不能调用data.test或者data.test.value，</p>
<p>否则会报错</p>
<p>因为，在JS中无法读取undefined身上的属性</p>
<p>因为undefined身上没有属性</p>
<p>也无法给undefined添加相对应的属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> data = &#123;test：&#123;value：<span class="number">1</span>&#125;&#125;</span><br><span class="line"><span class="keyword">if</span>（data &amp;&amp; data.test）&#123;</span><br><span class="line"> <span class="built_in">console</span>.log（data.test.value）;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>  </span><br><span class="line"><span class="string">``</span><span class="string">``</span></span><br><span class="line"></span><br><span class="line">幸运的是，使用可选链接的方法，你可以简单明了地编写上面的代码：</span><br><span class="line"></span><br><span class="line"><span class="string">``</span><span class="string">`js</span></span><br><span class="line"><span class="string">const value = data?.test?.value;</span></span><br><span class="line"><span class="string">console.log(value)  </span></span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>  </span><br></pre></td></tr></table></figure>

<p>现在，你还可以安全地尝试访问不存在的属性，而不会出现问题：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(data?.this?.does?.not?.exist?.for?.sure)  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">undefined</span>  </span><br></pre></td></tr></table></figure>

<h3 id="加餐：提高JSON的可读性"><a href="#加餐：提高JSON的可读性" class="headerlink" title="加餐：提高JSON的可读性"></a><strong>加餐：提高JSON的可读性</strong></h3><p>我确定你以前用过JSON.stringify()</p>
<p>但是，你可能不知道它的一种方法是可以对JSON数据格式进行整齐缩进的</p>
<p>让我们看看如何完成此任务。该JSON.stringify()方法接受两个可选参数：</p>
<ul>
<li>替换功能，用于过滤显示的JSON。在这种情况下，可以是null，因为我们不需要它。</li>
<li>一个空格值，可以是所需空格的数目或字符串。在这种情况下，让我们使用制表符（’\t’）缩进JSON对象以使其看起来不错：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> readableJSON = <span class="built_in">JSON</span>.stringify(&#123; <span class="attr">a</span>: <span class="string">&#x27;A&#x27;</span>, <span class="attr">b</span>: <span class="string">&#x27;B&#x27;</span> &#125;, <span class="literal">null</span>, <span class="string">&#x27;\t&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(readableJSON);  </span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"> <span class="string">&quot;a&quot;</span>: <span class="string">&quot;A&quot;</span>,</span><br><span class="line"> <span class="string">&quot;b&quot;</span>: <span class="string">&quot;B&quot;</span></span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">Vixcity</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2021/09/17/%E3%80%90JavaScript%E3%80%9110%E4%B8%AA%E6%B8%85%E6%99%B0%E5%AE%9E%E7%94%A8%E6%9B%B4%E6%98%BE%E4%B8%93%E4%B8%9A%E7%9A%84JavaScript%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/">http://vixcity.gitee.io/my-gitee-blog/2021/09/17/%E3%80%90JavaScript%E3%80%9110%E4%B8%AA%E6%B8%85%E6%99%B0%E5%AE%9E%E7%94%A8%E6%9B%B4%E6%98%BE%E4%B8%93%E4%B8%9A%E7%9A%84JavaScript%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://vixcity.gitee.io/my-gitee-blog" target="_blank">Vixcity</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/">代码片段</a><a class="post-meta__tags" href="/my-gitee-blog/tags/JS%E5%BA%93/">JS库</a><a class="post-meta__tags" href="/my-gitee-blog/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/my-gitee-blog/tags/ES6/">ES6</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/JS.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/09/18/%E3%80%90JavaScript%E3%80%91%E8%8A%B1%E9%87%8C%E8%83%A1%E5%93%A8%E7%9A%84%20JS%20%E8%A3%85%E9%80%BC%E5%A4%A7%E6%B3%95%EF%BC%8C%E7%9B%B4%E5%91%BC%E5%86%85%E8%A1%8C/"><img class="prev-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【JavaScript】花里胡哨的 JS 装逼大法，直呼内行</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/09/16/%E3%80%90CSS%E3%80%91%E5%A6%99%E7%94%A8%20background%20%E5%AE%9E%E7%8E%B0%E8%8A%B1%E5%BC%8F%E6%96%87%E5%AD%97%E6%95%88%E6%9E%9C/"><img class="next-cover" src="/my-gitee-blog/img/CSS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【CSS杂谈】妙用 background 实现花式文字效果</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/09/15/%E3%80%90JavaScript%E3%80%91%E5%87%A0%E4%B8%AAJavaScript%E6%9E%81%E7%9F%AD%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5%EF%BC%88%E4%BD%A0%E5%80%BC%E5%BE%97%E6%8B%A5%E6%9C%89%EF%BC%89/" title="【JavaScript】几个JavaScript极短日期时间代码片段（你值得拥有）"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-15</div><div class="title">【JavaScript】几个JavaScript极短日期时间代码片段（你值得拥有）</div></div></a></div><div><a href="/my-gitee-blog/2021/09/18/%E3%80%90JavaScript%E3%80%91%E8%8A%B1%E9%87%8C%E8%83%A1%E5%93%A8%E7%9A%84%20JS%20%E8%A3%85%E9%80%BC%E5%A4%A7%E6%B3%95%EF%BC%8C%E7%9B%B4%E5%91%BC%E5%86%85%E8%A1%8C/" title="【JavaScript】花里胡哨的 JS 装逼大法，直呼内行"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-18</div><div class="title">【JavaScript】花里胡哨的 JS 装逼大法，直呼内行</div></div></a></div><div><a href="/my-gitee-blog/2021/09/26/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91DOM%E8%8A%82%E7%82%B9%E7%AF%87/" title="【一行代码可以实现的事】DOM节点篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-26</div><div class="title">【一行代码可以实现的事】DOM节点篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/29/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%85%B6%E4%BB%96%E7%AF%87/" title="【一行代码可以实现的事】其他篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">【一行代码可以实现的事】其他篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/28/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%87%BD%E6%95%B0%E7%AF%87/" title="【一行代码可以实现的事】函数篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-28</div><div class="title">【一行代码可以实现的事】函数篇</div></div></a></div><div><a href="/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/" title="【一行代码可以实现的事】字符串篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-01</div><div class="title">【一行代码可以实现的事】字符串篇</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>